package admin.iss.nus.edu.sg;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;

import admin.iss.nus.edu.sg.LinearRegressionTable;
import sg.edu.nus.iss.stockex.LinearRegression;
import sg.edu.nus.iss.stockex.MovingAverage;
import sg.edu.nus.iss.stockex.RelativeStrengthIndex;
import sg.edu.nus.iss.stockex.StochasticOscillator;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;

public class transferData {

	/**
	 * @param args
	 */
	public static AWSCredentials getCredentials() {
		AWSCredentials credentials = new BasicAWSCredentials(
				"AKIAI4LDMCJYHSTVLF4Q",
				"WNOyyksEIg7z4V/BPapKvRkYJ9iUz1h9MtaaRt8j");
		return credentials;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		AmazonS3Client s3 = new AmazonS3Client(getCredentials());
		
		S3Object lr_output;
		DataInputStream di;
		BufferedReader br;
		for(int i=0;i<3;i++)
		{
			lr_output = s3.getObject("stockprocessing", "lr_s_output/part-r-0000"+i);
			di = new DataInputStream(lr_output.getObjectContent());
			br = new BufferedReader(new InputStreamReader(di));
			lrOutputTransfer(br, lr_output);
		}
		
		S3Object ma_output;
		for(int i=0;i<3;i++)
		{
			ma_output = s3.getObject("stockprocessing", "ma_s_output/part-r-0000"+i);
			di = new DataInputStream(ma_output.getObjectContent());
			br = new BufferedReader(new InputStreamReader(di));
			maOutputTransfer(br, ma_output);
		}
		S3Object rs_output; 
		for(int i=0;i<3;i++)
		{
			rs_output=s3.getObject("stockprocessing", "rsi_s_output/part-r-0000"+i);
			di=new DataInputStream(rs_output.getObjectContent());
			br=new BufferedReader(new InputStreamReader(di));
			rsOutputTransfer(br, rs_output);
		}
		
		S3Object so_output;
		for(int i=0;i<3;i++)
		{	
			so_output=s3.getObject("stockprocessing","so_s_output/part-r-0000"+i);
			di=new DataInputStream(so_output.getObjectContent());
			br=new BufferedReader(new InputStreamReader(di));
			soOutputTransfer(br, so_output);
			
		}		
		
	}
	public static void lrOutputTransfer(BufferedReader br,S3Object lr_output)
	{
		String str = "";
		LinearRegression linearRegression;
		LinearRegressionTable linearRegressionTable = new LinearRegressionTable();
		System.out.println("Starting");
		try {
			while ((str = br.readLine()) != null) {
				linearRegression = new LinearRegression(str);
				linearRegressionTable.Add(linearRegression);
			}
			lr_output.getObjectContent().close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		System.out.println("LR done");
	}
	public static void maOutputTransfer(BufferedReader br,S3Object ma_output)
	{
		String str = "";
		MovingAverage movingAverage;
		MovingAverageTable ma = new MovingAverageTable();
		try {
			while ((str = br.readLine()) != null) {
				movingAverage = new MovingAverage(str);
				ma.Add(movingAverage);
			}
			ma_output.getObjectContent().close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		System.out.println("MA done");
	}
	public static void rsOutputTransfer(BufferedReader br,S3Object rs_output)
	{
		String str="";
		RelativeStrengthIndex ri;
		RelativeStrengthIndexTable rsTable=new RelativeStrengthIndexTable();
		try{
			while((str=br.readLine())!=null)
			{
				ri=new RelativeStrengthIndex(str);
				rsTable.Add(ri);				
			}
			rs_output.getObjectContent().close();
			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
		System.out.println("RS done");
	}
	public static void soOutputTransfer(BufferedReader br,S3Object so_output)
	{
		String str="";
		StochasticOscillator so;
		StochasticOscillatorTable sot=new StochasticOscillatorTable();
		try{
			while((str=br.readLine())!=null)
			{
				so=new StochasticOscillator(str);
				sot.Add(so);				
			}
			so_output.getObjectContent().close();
			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
		System.out.println("SO done");

	}
	
}
